<style>
    .ewGridMiddlePanelHide {
        max-height: 95px;
        overflow: hidden;
    }
</style>
<script type="text/javascript">


   // $(".ewGridMiddlePanel").addClass("ewGridMiddlePanelHide");




</script>
<?php

//echo getMaxOrderObject("evigie_ao_inquery_status",null,"parentId",2);
$aoId=$currentObject->id;
//echo $aoId;
$action=getParameter("actionCode");

echo "Action :".$action;

$providerId=getParameter("providerId");
$evigie_ao_inqueryId=getParameter("evigie_ao_inqueryId");

$compnayName=getObjByKey("evigie_providers","companyName","id",$providerId);

function moveAOnextStage($statusCode)
{
    global $evigie_ao_inqueryId;
    //bizzar
    $evigie_ao_inqueryId=getParameter("evigie_ao_inqueryId");
    //update status of this tender
    $currentStatusId=getObjByKey("evigie_ao_inquery","inquery_status","id",$evigie_ao_inqueryId);
 //   $parentStatusId=getObjByKey("evigie_ao_inquery_status","parentId","id",$currentStatusId);

    $nextStatus=getObjByKey("evigie_ao_inquery_status","id","statusCode",$statusCode);
    if ($currentStatusId!=$nextStatus){

    updateObjByKey("evigie_ao_inquery","id",$evigie_ao_inqueryId,"inquery_status",$nextStatus);
    addInqueryHistory($evigie_ao_inqueryId,$nextStatus);

    ShowSuccess("Le statut de la requette a été mis à jour automatiquement.");
    }

}


function addInqueryHistory($evigie_ao_inqueryId,$statusId){
    global $portalId;

    insertObjByKey("evigie_inqueryHistory","id","inqueryId","statusId","date","portalId",NULL,$evigie_ao_inqueryId,$statusId,"NOW()",$portalId);
}


function addInquery($providerId,$productId,$aoId){
	global $portalId;
	$inquery_status=getObjByKey("evigie_ao_inquery_status","id","statusCode","01");
	insertObjByKey("evigie_ao_inquery","id","providerId","productId","portalId","inquery_status","aoId",'NULL',$providerId,$productId,$portalId,$inquery_status,$aoId);	
	$evigie_ao_inqueryId=getLastId();
	addInqueryHistory($evigie_ao_inqueryId,$inquery_status);
}

if ($action=="01")
{

    if (getParameter("subaction")=="phoneok")
    {
        moveAOnextStage("02");

    }
    elseif (getParameter("subaction")=="sendmail")
    {
        $mail=getParameter("mail");
        $mail=str_replace("\n","",$mail);
        $mail=str_replace("\r","",$mail);
        sendMailTo(getObjByKey("evigie_providers","mail","id",$providerId),$mail);


        ShowSuccess("Mail Envoyé à l'entreprise ".$compnayName);
        moveAOnextStage("02");
    }else{

  $mail=getObjByKey("evigie_providers","mail","id",$providerId);

    if ($mail=="")
    {
//contact by phone
        $phone=getObjByKey("evigie_providers","telephone","id",$providerId);

        $url=addParameter("subaction","phoneok");
        ?>

        Adresse mail non disponible , merci de proceder par appel telephonique : <?php  echo $phone; ?>.
        <br/>
        Merci de valider cette action à la fin de l'appel téléphonique. <a href="<?=$url?>">Valider</a>
    <?php

    }else{

        //contact by mail
//
        ?>
  
        <form action="<?php echo addParameter("subaction","sendmail"); ?>" method="POST">

            <?php

            $initialValue=getObjByKey("evigie_mailtemplates","template","name","contactcompany");
            makeRTE("mail",$initialValue);

            ?>

        <br/>

            <input type="submit" value="Envoyer le mail">
        </form>
    <?php


    }

    }


}elseif($action=="02"){

    $urlok=addParameter("actionCode","04");
    $url=addParameter("actionCode","05");
    ?>

    Type de reponse
    <br/>
    <a href="<?=$urlok?>">Intéréssé</a>
    <a href="<?=$url?>">Pas Intéréssé</a>



<?php



}elseif($action=="05"){

    moveAOnextStage("05");
    $url=addParameter("actionCode","05");
    ?>

Pas Intéréssé



<?php



}elseif($action=="04"){



    moveAOnextStage("04");
    
    
    
    
    
    if (getParameter("subaction")=="phoneok")
    {
    	 moveAOnextStage("11");
    
    }elseif (getParameter("subaction")=="sendmail")
    {
        $mail=getParameter("mail");
        $mail=str_replace("\n","",$mail);
        $mail=str_replace("\r","",$mail);
        sendMailTo(getObjByKey("evigie_providers","mail","id",$providerId),$mail);


        ShowSuccess("Mail Envoyé  l'entreprise ".$compnayName);
        moveAOnextStage("11");
    }else{

        $mail=getObjByKey("evigie_providers","mail","id",$providerId);

        if ($mail=="")
        {
//contact by phone
            $phone=getObjByKey("evigie_providers","telephone","id",$providerId);

            $url=addParameter("subaction","phoneok");
            ?>

        Adresse mail non disponible , merci de proceder par appel telephonique : <?php  echo $phone; ?>.
        <br/>
        Merci de valider cette action à la fin de l'appel téléphonique. <a href="<?=$url?>">Valider</a>
        <?php

        }else{

            //contact by mail
//
            ?>
        <form action="<?php echo addParameter("subaction","sendmail"); ?>" method="POST">

            <?php

            $initialValue=getObjByKey("evigie_mailtemplates","template","name","companyinterested");
            makeRTE("mail",$initialValue);

            ?>

            <br/>

            <input type="submit" value="Envoyer le mail">
        </form>
        <?php


        }

    }


}


if (getParameter("actionCode")!=null)
{

    ?>
    <br/>
    <?php

    //$url=addParameter("id",$aoId,"");
    
    $url=addParameter("ButterflyPortal_sys_rec",$aoId,"");
    $url=addParameter("ButterflyPortal_sys_operation","View",$url);

    ?><br/>
    <a href="<?php echo $url; ?>">Retour</a>
<?php
}
else{


//link providers with products in this tender

//get
$sSql="SELECT
evigie_provider_products.providerId,
evigie_provider_products.productId
FROM
evigie_ao_products
INNER JOIN evigie_provider_products ON evigie_ao_products.productId = evigie_provider_products.productId
where aoId=$aoId";

//echo $sSql;

$GLOBALS["conn"]->BeginTrans();

$rs=$GLOBALS["conn"]->Execute($sSql);
//$statusId=getObjByKey("evigie_ao_inquery_status","id","statusCode","01");
while ( !$rs->EOF)  {


    $providerId=$rs->fields["providerId"];
    $productId=$rs->fields["productId"];

   /* $sSql="INSERT IGNORE INTO evigie_ao_inquery values (NULL,$providerId,$productId,$portalId,$statusId,$aoId)";
    echo $sSql;
    $GLOBALS["conn"]->Execute($sSql);*/
    
   addInquery($providerId,$productId,$aoId);


   


    $rs->MoveNext();

}


$GLOBALS["conn"]->CommitTrans();

    $url=addParameter("ButterflyPortal_sys_rec", getParameter("ButterflyPortal_sys_rec"),"");
    $url=addParameter("ButterflyPortal_sys_operation", getParameter("ButterflyPortal_sys_operation"),$url);
    
?>

    <form action="<?=$url?>" method="POST" style="float: right;margin-bottom: 5px" id="filterform">
    
  <?php 
  $isInternational=true;
  
  if ($currentObject->type==1) $isInternational=false;
  
  if ($isInternational){ ?>  
    <select name="countryId" id="countries" style="height: 32px">
    <option value="">Séléctionnez un pays</option>
   <?php

$sSql="SELECT * FROM portal_countries";

//echo $sSql."<br/>";

$frs=$GLOBALS["conn"]->Execute($sSql);

while ($frs && !$frs->EOF)  {
    $countryId=$frs->fields["id"];
    $countryCode=$frs->fields["geoCode"];
    $countryLib=$frs->fields["lib_".$LANG];
    $selected="";
if ($countryId==getParameter("countryId")) $selected="selected";
?>


 <option <?=$selected?>  value="<?php echo $countryId;  ?>" code="<?php echo $countryCode;  ?>" ><?php echo $countryLib;  ?></option>
    <br/>
    <?php

    $frs->MoveNext();


}
?>
        </select>
        <?php }
        else{
        ?>

        
          <select name="regionId"  id="regionId" style="height: 32px">
    
   <?php

$sSql="SELECT
portal_regions.*
FROM
portal_countries
INNER JOIN portal_regions ON portal_regions.countryId = portal_countries.id
WHERE
portal_countries.geoCode = 'MAR'";

//echo $sSql."<br/>";

$frs=$GLOBALS["conn"]->Execute($sSql);

while ($frs && !$frs->EOF)  {
    $regionId=$frs->fields["id"];
    $regionCode=$frs->fields["geoCode"];
    $regionLib=$frs->fields["lib_".$LANG];
    $selected="";
if ($regionId==getParameter("regionId")) $selected="selected";
?>


 <option <?=$selected?>  value="<?php echo $regionId;  ?>" ><?php echo $regionLib;  ?></option>
    <br/>
    <?php

    $frs->MoveNext();


}
?>
        </select>
        
        <?php }?>

        <select name="productId" id="products" onchange="this.form.submit()" style="height: 32px">
<?php

$sSql="SELECT
evigie_products.id, evigie_products.code, evigie_products.lib_$LANG as lib,evigie_ao_products.id as ao_productid
FROM
evigie_ao_products
INNER JOIN evigie_products ON evigie_ao_products.productId = evigie_products.id
WHERE
evigie_ao_products.aoId = $aoId";

//echo $sSql."<br/>";

$frs=$GLOBALS["conn"]->Execute($sSql);
$fproductId=null;
while ($frs && !$frs->EOF)  {
    $productId=$frs->fields["id"];
    if ($fproductId==null) $fproductId=$productId;
    $productCode=$frs->fields["code"];
    $productLib=$frs->fields["lib"];
    $selected="";
if ($productId==getParameter("productId")) $selected="selected";
?>


 <option <?=$selected?> value="<?=$productId?>" code="<?php echo $productCode;  ?>" ><?php echo $productCode;  ?> <?php echo $productLib;  ?></option>
    <br/>
    <?php

    $frs->MoveNext();


}
?>
        </select>

        <a onclick="findproviders()">
            <img  width="32px" style="vertical-align: bottom" title="Recherche la liste des fournisseurs de ce produit sur Kompass" src="./images/kompass.png"></a>
        <a onclick="togglefilter()"><img  style="vertical-align: bottom" width="32px" title="Filtre par statut" src="./images/icons/filter.png"></a>
        
        <script type="text/javascript">

        function findproviders(){

        	productId=$('#products').val();
        	productCode=$('#products>option:selected').attr("code");
        	countryCode=$('#countries>option:selected').attr("code");
	
        window.location.replace("/portlet.php?app=evigieutils&operation=searchkompass&countryCode="+countryCode+"&productCode="+productCode+"&productId="+productId+"&aoId=<?=$aoId?>");
    	    
        }

        $( "#countryId" ).change(function() {
        	  $("#filterform").submit();
        });
        </script>

 <div id="filter" class="filter">
            <?php

            function printEvigiestatus($_parentId){

                global $LANG;
               $op="=";

                if ($_parentId=="NULL") $op="is";

    $sSql="SELECT
    evigie_ao_inquery_status.id,
    evigie_ao_inquery_status.parentId,
evigie_ao_inquery_status.statusCode,
evigie_ao_inquery_status.lib_$LANG as statusLib,
evigie_ao_inquery_status.orderCmpnt,
evigie_ao_inquery_status.Color
 FROM `evigie_ao_inquery_status` where parentId $op $_parentId ORDER BY orderCmpnt asc;";

                $inStatus="";
    $srs=$GLOBALS["conn"]->Execute($sSql);
            ?>
        <ul>
        <?php
    while ( !$srs->EOF)  {
        $statusId=$srs->fields["id"];
        $parentId=$srs->fields["parentId"];
        $statusLib=$srs->fields["statusLib"];
        $checked="";
       if(getParameter("status_".$statusId)!=null) { $checked="checked='checked'"; $inStatus.=getParameter("status_".$statusId).", "; };
        ?>
       <li> <input id="parent_<?=$parentId?>"type="checkbox" value="<?=$statusId?>" <?=$checked?> name="status_<?=$statusId?>"><?=$statusLib?></li>

        <?php
        $srs->MoveNext();
        $inStatus.=printEvigiestatus($statusId);

    }

        ?>
            </ul>
            <?php



                return $inStatus;
            }
            $inStatus="and evigie_ao_inquery.inquery_status in (";
            $inStatus.=printEvigiestatus("NULL");
            $inStatus.="-1)";
        ?>
     <input type="submit" value="Filtrer" />
 </div>
        <script type="text/javascript">
            function togglefilter(){
                $("#filter").toggle("fast");

                $("#providertable").toggle("fast");

            }
            $("#filter").toggle("fast");
        </script>

</form>
    <br/>
<table cellspacing="0" rowhighlightclass="x" rowselectclass="ewTableSelectRow" roweditclass="ewTableEditRow" class="ewTable ewTableSeparate" id="providertable">

<thead>
    <tr class="ewTableHeader">
        <td width="20px" style="text-align: center"></td> <td width="200px" style="text-align: center">Status</td>  <td style="text-align: center">Logo</td> <td width="70%">Fournisseur</td> <td style="text-align: center">Pays</td> <td style="text-align: center">Action</td><td>Modifier</td><td>Historique</td>
        </tr>
</thead>
    <tbody>
<?php

$countryId=getParameter("countryId");
//$countryId=getObjByKey("portal_countries","id","geoCode",$countryId);

if ($countryId)
$countryWhere=" evigie_providers.countryId=$countryId AND";
else 
$countryWhere="";
	
//echo $countryId;


$productId=getParameter("productId");
if ($productId==null) $productId=$fproductId;
if( $inStatus=="and evigie_ao_inquery.inquery_status in (-1)")  $inStatus="";
$sSql="SELECT
evigie_ao_inquery.id as evigie_ao_inqueryId,
evigie_providers.*,
evigie_ao_inquery_status.Color as statusColor,
evigie_ao_inquery_status.statusCode,
evigie_ao_inquery_status.lib_$LANG as statusLib,
portal_countries.lib_$LANG as addressCountry,
portal_cities.lib_$lt as addressLocality
FROM
evigie_ao_inquery
INNER JOIN evigie_ao_inquery_status ON evigie_ao_inquery.inquery_status = evigie_ao_inquery_status.id
INNER JOIN evigie_providers ON evigie_ao_inquery.providerId = evigie_providers.id
INNER JOIN portal_cities ON evigie_providers.cityId = portal_cities.id
INNER JOIN portal_countries ON evigie_providers.countryId = portal_countries.id
WHERE
$countryWhere
evigie_ao_inquery.productId = $productId  AND
evigie_ao_inquery.aoId = $aoId $inStatus";

//echo $sSql;

$rs=$GLOBALS["conn"]->Execute($sSql);
$i=0;
while ($rs && !$rs->EOF)  {


    $logo=$rs->fields["logo"];
    if ($logo=="") $logo="no-logo.png";
    $providerId=$rs->fields["id"];
    $companyName=$rs->fields["companyName"];
    $addressCountry=$rs->fields["addressCountry"];
    $statusLib=$rs->fields["statusLib"];
    $flag=getObjByKey("portal_countries","flag","lib_FR",$addressCountry);
    $evigie_ao_inqueryId=$rs->fields["evigie_ao_inqueryId"];
    $statusColor=$rs->fields["statusColor"];
    $statusCode=$rs->fields["statusCode"];



  /*  $sql="update evigie_providers set logo='$logo' where id=".$rs->fields["id"];
    $GLOBALS["conn"]->Execute($sql);*/
    
    $url=addParameter("ButterflyPortal_sys_rec",$aoId);
    $url=addParameter("ButterflyPortal_sys_operation","View",$url);
    $url=addParameter("actionCode",$statusCode,$url);
    $url=addParameter("providerId",$providerId,$url);

      $url=addParameter("evigie_ao_inqueryId",$evigie_ao_inqueryId,$url);
   
    //ewTableLastRow
    $i++;
    $class="oddline";
    if ($i%2)    $class="evenline";
    ?>
<tr class="<?=$class?>  ewTableRow">
    <td width="20px" style="text-align: center"><div class="workflow_status" style="background-color:<?=$statusColor?>"></div></td>
    <td style="white-space:nowrap; text-align: center"><?php echo $statusLib; ?></td>
    <td><img src="<?php echo getFilesPath("logos",false)."/".$logo;  ?>" height="30px" width="80px"></td>
    <td ><?php echo $companyName;  ?></td>
    <td style="white-space:nowrap; text-align: center"><img width="40px" src="/images/flags/<?php echo $flag; ?>" title="<?php echo $addressCountry;  ?>"></td>
    <td  style="text-align: center"><a href="<?php echo $url; ?>"><img src="./images/icons/mail_phone.png" title="Contacter l'entreprise"></a></td>
    <td  style="text-align: center"><a href="?table=evigie_providers&ButterflyPortal_sys_operation=Change&ButterflyPortal_sys_rec=<?php echo $providerId; ?>" ><img border="0" src="./images/icons/ButterflyPortal-change.png" title="Modifier"></a></td>
    <td style="text-align: center"><a href="?table=evigie_inqueryhistory&filter=inqueryId=<?=$evigie_ao_inqueryId?>"><img  width="32px" src="./images/evigie_inqueryhistory.png"></a></td>

</tr>

    <?php
    $rs->MoveNext();

    }



    ?>
    </tbody>

</table>


    <?php




}
?>
<br clear="all"/><br clear="all"/>